package com.muke.utils;

import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ClassName DateUtils
 * @Description: TODO
 * @Author hezx
 * @Date 2019-10-9
 * @Version V1.0
 **/
public class DateUtils {
    //默认时间格式
    public static final String DATE_DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static SimpleDateFormat sdf = new SimpleDateFormat();

    /***
     * @Description: 字符串时间格式转换为 Date 类型
     * @Author: hezx
     * @Date: 2019-10-9
     * @param dateStr 字符串时间格式
     * @param format 格式，可不填写
     * @return: void
     */
    public static Date strToDate(String dateStr, String format) {
        if (StringUtils.isEmpty(dateStr))
            return null;
        Date date = null;
        if (StringUtils.isEmpty(format))
            format = DATE_DEFAULT_FORMAT;
        sdf = new SimpleDateFormat(format);
        try {
            date = sdf.parse(dateStr);
        } catch (ParseException e) {
            //e.printStackTrace();
            return date;
        }
        return date;
    }

    public static void main(String[] args) {
        Date date = DateUtils.strToDate("2019-09-30 12:00:00", null);
        System.out.println("=========date:" + date);
    }
}
